package top.chmin.project.cloud.sys.table.info;

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.Page;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import top.chmin.project.cloud.sys.dict.data.DictData;
import top.chmin.project.cloud.sys.dict.data.DictDataCondition;



/**
 * chmin
 * 2024-01-25 14:19:37
 * cloud-code
 */
@Service
@Transactional
public class TableInfoService {

    @Resource
    TableInfoPlugin plugin;

    public int save(TableInfoSaveRest.P p) {
        plugin.checkSave(p);
        TableInfo tableInfo = BeanUtil.toBean(p, TableInfo.class);
        plugin.beforeSave(tableInfo);
        int result = tableInfo.forInsert().execute();
        plugin.afterSave(p, tableInfo, result);
        return result;
    }

    public TableInfoSelectRest.R select(TableInfoSelectRest.P p) {
        plugin.checkSelect(p);
        TableInfoCondition c = TableInfoCondition.forSelect().id().eq(p.getId());
        plugin.beforeSelect(p, c);
        TableInfoSelectRest.R r = c.selectFirst(TableInfoSelectRest.R.class);
        plugin.afterSelect(p, r);
        return r;
    }

    public int update(TableInfoUpdateRest.P p) {
        plugin.checkUpdate(p);
        TableInfo tableInfo = BeanUtil.toBean(p, TableInfo.class);
        plugin.beforeUpdate(tableInfo);
        int result = tableInfo.forUpdate().id().eq().execute();
        plugin.afterUpdate(p, tableInfo, result);
        return result;
    }

    public int delete(TableInfoDeleteRest.P p) {
        plugin.checkDelete(p);
        TableInfo tableInfo = BeanUtil.toBean(p, TableInfo.class);
        plugin.beforeDelete(tableInfo);
        int result = tableInfo.forDelete().id().eq().execute();
        plugin.afterDelete(p, tableInfo, result);
        return result;
    }

    public Page<TableInfoPageRest.R> page(TableInfoPageRest.P p) {
        plugin.checkPage(p);
        TableInfoCondition c = TableInfoCondition.forSelect();
        c = p.isDesc() ? c.id().desc() : c.id().asc();
        plugin.beforePage(p, c);
        Page<TableInfoPageRest.R> r = c.selectPage(TableInfoPageRest.R.class);
        /*###########auto#start###########*/
        /*###########auto##end############*/
        /* append $serviceRelateAppender$ */
        /*###########auto#start###########*/
        /*###########auto##end############*/
        /* append $serviceDictAppender$ */
        plugin.afterPage(p, r);
        return r;
    }
    /* append $serviceSelectOptionMethodAppender$ */
}
